﻿using System;
using System.Collections.Generic;

namespace RStudio.UIFramwork
{
    /// <summary>
    /// 事件驱动管理
    /// </summary>
    /// <remarks>
    /// 
    /// </remarks>
    public class UIModuleEvent
    {
        public UIModuleEvent()
        {
            this.PreLoadActions = new();
            this.UnLoadActions = new();

            this.GlobalPreLoadActions = new();
            this.GlobalUnLoadActions = new();
        }


        /// <summary>
        /// 事件|当页面激活
        /// </summary>
        public Dictionary<Type, List<Action<UIBase>>> PreLoadActions { get; private set; }

        /// <summary>
        /// 事件|当页面卸载
        /// </summary>
        public Dictionary<Type, List<Action<UIBase>>> UnLoadActions { get; private set; }

        /// <summary>
        /// 全局事件|当页面激活
        /// </summary>
        public List<Action<UIBase>> GlobalPreLoadActions { get; private set; }

        /// <summary>
        /// 全局事件|当页面卸载
        /// </summary>
        public List<Action<UIBase>> GlobalUnLoadActions { get; private set; }

        /// <summary>
        /// 事件|指定添加
        /// </summary>
        public void AddAction<T>(Action<T> action, UIBaseEvent uiBaseEvent) where T : UIBase
        { 
            Type type = typeof(T);
            var target = Get(uiBaseEvent);
            bool isContain = target.ContainsKey(type);

            if (!isContain) {
                target.Add(type, new List<Action<UIBase>>());
            }

            target[type].Add(action as Action<UIBase>);
        }

        /// <summary>
        /// 事件|全局添加
        /// </summary>
        /// <param name="action"></param>
        /// <param name="uiBaseEvent"></param>
        public void AddAction(Action<UIBase> action, UIBaseEvent uiBaseEvent) 
        {
            var target = GetGlobal(uiBaseEvent);
            bool isContain = target.Contains(action);

            if (!isContain)
            {
                target.Add(action);
            }
        }

        /// <summary>
        /// 事件预载|删除
        /// </summary>
        public void RemoveAction<T>(Action<T> action, UIBaseEvent uiBaseEvent) where T : UIBase
        {
            Type type = typeof(T);
            var target = Get(uiBaseEvent);
            bool isContain = target.ContainsKey(type);

            if (!isContain) return;

            target[type].Remove(action as Action<UIBase>);

            if (target[type].Count == 0)
            {
                target.Remove(type);
            }
        }



        public void DoLoad(UIBase uiBase)
        { 
            Type type = uiBase.GetType();
            var actions = Get(UIBaseEvent.ActiveUI)[type];

            foreach (var item in actions)
            {
                uiBase.OnUIActived += item;
            }

            actions = GetGlobal(UIBaseEvent.ActiveUI);
            foreach (var item in actions)
            {
                uiBase.OnUIActived += item;
            }
        }
        public void DoUnLoad(UIBase uiBase)
        {
            Type type = uiBase.GetType();
            var actions = Get(UIBaseEvent.CreateUI)[type];

            foreach (var item in actions)
            {
                uiBase.OnUIActived -= item;
            }
        }



        /// <summary>
        /// 获取|指定注册的事件行为
        /// </summary>
        /// <param name="uiBaseEvent"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private Dictionary<Type, List<Action<UIBase>>> Get(UIBaseEvent uiBaseEvent)
        {
            return uiBaseEvent switch
            {
                UIBaseEvent.CreateUI => throw new NotImplementedException(),
                UIBaseEvent.ActiveUI => this.PreLoadActions,
                UIBaseEvent.StartIUI => throw new NotImplementedException(),
                UIBaseEvent.DeActiveUI => this.UnLoadActions,
                UIBaseEvent.DisposeUI => throw new NotImplementedException(),
                _ => throw new NotImplementedException(),
            };
        }

        /// <summary>
        /// 获取|全局注册的事件行为
        /// </summary>
        /// <param name="uiBaseEvent"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private List<Action<UIBase>> GetGlobal(UIBaseEvent uiBaseEvent)
        {
            return uiBaseEvent switch
            {
                UIBaseEvent.CreateUI => throw new NotImplementedException(),
                UIBaseEvent.ActiveUI => this.GlobalPreLoadActions,
                UIBaseEvent.StartIUI => throw new NotImplementedException(),
                UIBaseEvent.DeActiveUI => this.GlobalUnLoadActions,
                UIBaseEvent.DisposeUI => throw new NotImplementedException(),
                _ => throw new NotImplementedException(),
            };
        }
    }

    public enum UIBaseEvent
    { 
        CreateUI,
        ActiveUI,
        StartIUI,
        DeActiveUI,
        DisposeUI
    }


    public class UIBaseNotice
    {
        public Action OnCreate;
        public Action OnActive;
        public Action OnDeActive;
        public Action OnDispose;
    }
}


